package com.frost;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class Test2 {


    // 注意：逗号后面不能有空格
    private String connectString = "centos102:2181,centos103:2181,centos104:2181";
    private int sessionTimeout = 2000;
    private ZooKeeper zkClient;

    // 创建客户端
    @Before
    public void init() throws IOException {

        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
//
//                //节点路径
//                String path = event.getPath();
//                //事件类型
//                Event.EventType eventType = event.getType();
//                //客户端状态
//                Event.KeeperState eventState = event.getState();

            }
        });
    }

    //判断节点是否存在并注册一个Watcher监听
    @Test
    void exist2() throws InterruptedException, KeeperException {
        //判断节点是否存在，并注册监听事件，注意：一个监听事件只能触发一次，如果需要多次触发
        //需要第一个监听事件触发后再注册一个监听事件
        Stat exists = zkClient.exists("/locks", new Watcher() {
            //如果该节点发生变化，比如创建，或者删除时，就会回调该方法
            @Override
            public void process(WatchedEvent event) {
                //节点路径
                String path = event.getPath();
                System.out.println(path);
                //事件类型
                Event.EventType eventType = event.getType();
                //客户端状态
                Event.KeeperState eventState = event.getState();
                switch (eventType) {
                    case None:
                        System.out.println("客户端连接状态发生改变");
                        break;
                    case NodeCreated:
                        System.out.println(path+"节点创建了");
                        break;
                    case NodeDeleted:
                        System.out.println(path+"节点删除了");
                        break;
                    case NodeDataChanged:
                        System.out.println(path+"节点数据发生改变了");
                        break;
                    case NodeChildrenChanged:
                        System.out.println(path+"节点的子节点发生改变了");
                        break;
                }
                switch (eventState) {
                    case Disconnected:
                        System.out.println("客户端断开连接");
                        break;
                    case SyncConnected:
                        System.out.println("客户端连接成功");
                        break;
                    case AuthFailed:
                        System.out.println("客户端身份验证失败");
                        break;
                    case ConnectedReadOnly:
                        System.out.println("客户端以只读连接");
                        break;
                    case SaslAuthenticated:
                        System.out.println("客户端身份验证通过");
                        break;
                    case Expired:
                        System.out.println("客户端过期");
                        break;
                }
            }
        });

        //程序休眠一段时间，否则程序结束后就收不到回调信息
        Thread.sleep(300000);
    }


}
